package org.colin.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DEntry;
import org.colin.dictPojo.DStudyLevel;

import org.colin.model.vo.resp.EchartsVO;
import org.colin.service.DEntryService;
import org.colin.service.DStudyLevelService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Controller
@RequestMapping("/dStudyLevel")
@Api(tags = "基础数据管理-学习等级管理")
public class DStudyLevelController {


    @Autowired
    DStudyLevelService dStudyLevelService;
    @Autowired
    DEntryService dEntryService;


    @ApiOperation(value = "新增学习等级接口")
    @LogAnnotation(title = "基础数据管理", action = "新增学习等级接口")
    @PostMapping("addStudyLevel")
    @ResponseBody

    public DataResult addStudyLevel(@ApiParam(name="学习等级类",value="学习等级对象",required=true) DStudyLevel dStudyLevel){
        String levelName = dStudyLevel.getLevelName();
        if(levelName == null || levelName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,等级名称不能为空");

        }
        QueryWrapper<DStudyLevel> dStudyLevelQueryWrapper = new QueryWrapper<>();
        dStudyLevelQueryWrapper.eq("levelName",levelName);
        int count = dStudyLevelService.count(dStudyLevelQueryWrapper);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,等级名称不能重复");
        }
        dStudyLevel.setCreateTime(LocalDateTime.now());
        boolean save = dStudyLevelService.save(dStudyLevel);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败");
        }
    }


    @ApiOperation(value = "修改学习等级接口")
    @PutMapping("updateStudyLevel")
    @LogAnnotation(title = "基础数据管理", action = "修改学习等级接口")
    @ResponseBody

    public DataResult updateStudyLevel(@ApiParam(name="学习等级类",value="对象",required=true) @RequestBody @Valid  DStudyLevel dStudyLevel){
        Integer id = dStudyLevel.getId();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"修改失败,唯一标识不能为空");
        }
        int count = dEntryService.selectCountByLevel(id);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"修改失败,学习等级下有试题，禁止修改");
        }
//        String levelName = dStudyLevel.getLevelName();
//        if(levelName == null || levelName.equals("")){
//            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"修改失败,等级名称不能为空");
//        }

        QueryWrapper<DStudyLevel> dStudyLevelQueryWrapper = new QueryWrapper<>();
//        dStudyLevelQueryWrapper.eq("levelName",levelName);
        dStudyLevelQueryWrapper.ne("id" , id);
        int count1 = dStudyLevelService.count(dStudyLevelQueryWrapper);
        if(count > 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,等级名称不能重复");
        }
        boolean save = dStudyLevelService.updateById(dStudyLevel);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"修改失败");
        }
    }

    @ApiOperation(value = "批量删除学习等级接口")
    @LogAnnotation(title = "基础数据管理", action = "批量删除学习等级接口")
    @DeleteMapping("deleteStudyLevel")
    @ResponseBody

    public DataResult deleteBatchStudyLevel(@ApiParam(name="id",value="[1,2,3]",required=true)@RequestBody String id){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败,唯一标识不能为空");
        }
        Integer delete = dStudyLevelService.deleteBatchStudyLevel(ids);
        if(delete == 1){
            return DataResult.success();
        }else if(delete == 3){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败,该等级下有词条存在，禁止删除");
        }else if(delete == 4){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败,该等级下有题目存在，禁止删除");
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败");

        }
    }


    @ApiOperation(value = "查询学习等级详情")
    @LogAnnotation(title = "基础数据管理", action = "查询学习等级详情")
    @GetMapping("selectStudyLevelById")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "唯一标识", name = "id" , paramType = "query",required=true),
    })
    @ResponseBody
    public DataResult selectStudyLevelById(Integer id){
        if(id == null || id.equals("")){
            return   new DataResult<>(BaseResponseCode.OPERATION_ERROR.getCode() , "查询失败,唯一标识不能为空");
        }
        DStudyLevel byId = dStudyLevelService.getById(id);
        DataResult<DStudyLevel> result = DataResult.success();
        result.setData(byId);
        return result;
    }


    @ApiOperation(value = "查询学习等级-分页")
    @LogAnnotation(title = "基础数据管理", action = "查询学习等级-分页")
    @GetMapping("selectStudyLevelIsPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="当前页" ,required=true, paramType = "query"),
            @ApiImplicitParam(name="pageSize",value="每页数量" ,required=true, paramType = "query")
    })
    @ResponseBody
    public DataResult selectStudyLevelIsPage(Integer page , Integer pageSize ){
        Page<DStudyLevel> dStudyLevels = new Page<>(page, pageSize);
        IPage<DStudyLevel> list = dStudyLevelService.page(dStudyLevels);
        DataResult<IPage<DStudyLevel>> result = DataResult.success();
        result.setData(list);
        return result;
    }


    @ApiOperation(value = "查询学习等级--不分页")
    @GetMapping("selectStudyLevelIsNotPage")
    @LogAnnotation(title = "基础数据管理", action = "查询学习等级--不分页")
    @ResponseBody
    public DataResult selectStudyLevelIsNotPage(){
        List<DStudyLevel> list = dStudyLevelService.list();

        DataResult<List<DStudyLevel>> result = DataResult.success();
        result.setData(list);
        return result;
    }





}

